package com.css.modules.elasticSearch.service.impl;

import com.alibaba.fastjson.JSON;
import com.css.modules.elasticSearch.entity.LawRepoEntity;
import com.css.modules.elasticSearch.repository.LawRepoRepository;
import com.css.modules.elasticSearch.service.EsSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * elasticsearch 搜索引擎 service实现
 * @author zhoudong
 * @version 0.1
 * @date 2018/12/13 15:33
 */
@Service
public class EsSearchServiceImpl extends BaseSearchServiceImpl<LawRepoEntity> implements EsSearchService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private LawRepoRepository lawRepoRepository;

    @Override
    public void save(LawRepoEntity... productDocuments) {
        elasticsearchTemplate.putMapping(LawRepoEntity.class);
        if(productDocuments.length > 0){
            /*Arrays.asList(productDocuments).parallelStream()
                    .map(productDocumentRepository::save)
                    .forEach(productDocument -> log.info("【保存数据】：{}", JSON.toJSONString(productDocument)));*/
            log.info("【保存索引】：{}", JSON.toJSONString(lawRepoRepository.saveAll(Arrays.asList(productDocuments))));
        }
    }

    @Override
    public void delete(String id) {
        lawRepoRepository.deleteById(id);
    }

    @Override
    public void deleteAll() {
        lawRepoRepository.deleteAll();
    }

    @Override
    public LawRepoEntity getById(String id) {
        return lawRepoRepository.findById(id).get();
    }

    @Override
    public List<LawRepoEntity> getAll() {
        List<LawRepoEntity> list = new ArrayList<>();
        lawRepoRepository.findAll().forEach(list::add);
        return list;
    }




}
